package com.hengpeng.itfin.persist.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfin.domain.vo.AccountLogVO;
import com.hengpeng.itfin.persist.CommonJdbcdao;
import com.hengpeng.itfin.persist.StaticJDBCDao;
import com.hengpeng.itfinbase.en.AccountType;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.AccountLog;
import com.hengpeng.itfinbase.persist.AccountLog.TradeMode;
import com.hengpeng.itfinbase.persist.AccountLogReport;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.ReceivePlan.ReceiveStatus;
import com.hengpeng.itfinbase.persist.RepaymentPlan;

@Repository
@Lazy
public class StaticJDBCDaoImpl implements StaticJDBCDao
{
	@Autowired
	private CommonJdbcdao dao;

	@Override
	public Account getPlatformAccount()
	{
		DynamicQuery query = new DynamicQuery(Account.class);
		query.eq("accountType", AccountType.PERSONAL.toString());
		query.addProjection(Projections.property("IFNULL(SUM(baseMoney),0)", "baseMoney"));
		query.addProjection(Projections.property("IFNULL(SUM(freezeMoney),0)", "freezeMoney"));
		query.addProjection(Projections.property("'BASEDT'", "subAccountType"));
		query.addProjection(Projections.property("'BASEDT'", "subAccountId"));
		query.setQueryType(QueryType.JDBC);
		return dao.getByDynamicQuery(query, Account.class);
	}

	@Override
	public AccountLogReport getPlatformSpedtAccount(String[] transTypes)
	{
		DynamicQuery query = new DynamicQuery(AccountLogReport.class);
		query.eq("accountType", AccountType.PLATFORM.toString());
		query.inArray("transType", transTypes);
		query.addProjection(Projections.property("IFNULL(SUM(transMoney),0)", "transMoney"));
		query.setQueryType(QueryType.JDBC);
		return dao.getByDynamicQuery(query, AccountLogReport.class);
	}

	@Override
	public List<AccountLogVO> getPlatAccountTrade(Date start, Date end, String[] transType)
	{
		DynamicQuery query = new DynamicQuery(AccountLog.class);
		query.eq("accountType", AccountType.PERSONAL.toString());
		query.inArray("tradeMode", new String[] { TradeMode.SUBTRACT.toString(), TradeMode.ADD.toString() });
		query.ge("transTime", start);
		query.lt("transTime", end);
		query.inArray("transType", transType);
		query.addProjection(Projections.property("IFNULL(SUM(money),0)", "money"));
		query.addProjection(Projections.property("transType", "transType"));
		query.addProjection(Projections.property("DATE_FORMAT(transTime,'%Y-%m-%d')", "reportTime"));
		query.addProjection(Projections.property("tradeMode", "tradeMode"));
		query.setQueryType(QueryType.JDBC);
		query.addGourp("DATE_FORMAT(transTime,'%Y-%m-%d')");
		query.addGourp("transType");
		query.addGourp("tradeMode");
		query.desc("transTime");
		return dao.findEntityByDynamicQuery(query, AccountLogVO.class);
	}

	@Override
	public RepaymentPlan getTotleRepay(Long borrowId)
	{
		DynamicQuery query = new DynamicQuery(RepaymentPlan.class);
		query.eq("borrowInfoId", borrowId);
		query.addProjection(Projections.property("IFNULL(SUM(repayBaseMoney),0)", "repayBaseMoney"));
		query.addProjection(Projections.property("IFNULL(SUM(repayInterestMoney),0)", "repayInterestMoney"));
		query.addProjection(Projections.property("IFNULL(SUM(repayAddMoney),0)", "repayAddMoney"));
		query.setQueryType(QueryType.JDBC);
		return dao.getByDynamicQuery(query, RepaymentPlan.class);
	}

	@Override
	public ReceivePlan getTotleReceive(Long investInfoId, Long userId)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		query.eq("investmentInfoId", investInfoId);
		query.eq("investUserId", userId);
		query.eq("status", ReceiveStatus.WAITREC.toString());
		query.addProjection(Projections.property("IFNULL(SUM(recBaseMoney),0)", "recBaseMoney"));
		query.addProjection(Projections.property("IFNULL(SUM(recInterestMoney),0)", "recInterestMoney"));
		query.addProjection(Projections.property("IFNULL(SUM(recAddMoney),0)", "recAddMoney"));
		query.setQueryType(QueryType.JDBC);
		return dao.getByDynamicQuery(query, ReceivePlan.class);
	}

	@Override
	public List<ReceivePlan> getTotleList(Long borrowId)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		query.eq("borrowInfoId", borrowId);
		query.addProjection(Projections.property("SUM(recbasemoney+recInterestMoney)", "recMoney"));
		query.addProjection(Projections.property("SUM(recbasemoney)", "recBasemoney"));
		query.addProjection(Projections.property("SUM(recAddMoney)", "recAddMoney"));
		query.addProjection(Projections.property("SUM(recInterestMoney)", "recInterestMoney"));
		query.addProjection(Projections.property("curRecnum", "curRecnum"));
		query.addProjection(Projections.property("totalRecNum", "totalRecNum"));
		query.addProjection(Projections.property("arrivalTime", "arrivalTime"));
		query.addGourp("curRecnum");
		query.addGourp("arrivalTime");
		query.addGourp("totalRecNum");
		query.asc("curRecnum");
		query.setQueryType(QueryType.JDBC);
		return dao.findEntityByDynamicQuery(query, ReceivePlan.class);
	}
}
